import { prisma } from '@/lib/prisma/client'
import { NextResponse } from 'next/server'

export async function POST(request: Request) {
  try {
    const { sceneIds } = await request.json()

    if (!Array.isArray(sceneIds)) {
      throw new Error('无效的场景ID列表')
    }

    const scenes = await prisma.scene.findMany({
      where: {
        id: {
          in: sceneIds
        }
      },
      select: {
        name: true,
        conditions: true,
        actions: true,
        isActive: true,
        devices: {
          select: {
            id: true,
            name: true,
            type: true
          }
        }
      }
    })

    // 设置响应头
    const headers = new Headers()
    headers.set('Content-Type', 'application/json')
    headers.set(
      'Content-Disposition',
      `attachment; filename="scenes_batch_${new Date().toISOString().split('T')[0]}.json"`
    )

    return new Response(JSON.stringify(scenes, null, 2), { headers })
  } catch (error) {
    return NextResponse.json(
      { error: '导出场景失败' },
      { status: 500 }
    )
  }
} 